using System.Collections;

namespace AssetImporter
{
    class CachedTransform<T, U> where T : notnull
    {
        private Dictionary<T, U> dict;
        private Func<T, U> tranform;
        public CachedTransform(Func<T, U> t)
        {
            tranform = t;
            dict = new Dictionary<T, U>();
        }

        public U Update(in T key)
        {
            var value = tranform(key);
            if(dict.ContainsKey(key))
                dict[key] = value;
            else
                dict.Add(key, value);
            return value;
        }

        public U Get(in T key)
        {
            if(dict.ContainsKey(key))
            {
                return dict[key];
            }
            else
            {
                var value = tranform(key);
                dict.Add(key, value);
                return value;
            }
        }
    }
}